//
//  accountBookVC.m
//  CMAccountBook
//
//  Created by 王开政 on 2018/10/30.
//  Copyright © 2018年 王开政. All rights reserved.
//

#import "accountBookVC.h"
#import "calendarVC.h"
#import "accountBookDetailVC.h"

@interface accountBookVC ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *Tab;

@property(nonatomic,strong)UIView *TabHeadView;

@property(nonatomic,strong)UILabel *titleLabel;

@property(nonatomic,strong)UIButton *rightButton;

@property(nonatomic,strong)UILabel *dataLabel;
@property(nonatomic,strong)UIButton *dataBtn;
@property(nonatomic,strong)UILabel *incomeLabel;
@property(nonatomic,strong)UILabel *income;
@property(nonatomic,strong)UILabel *expendLabel;
@property(nonatomic,strong)UILabel *expend;

@property(nonatomic,strong)NSMutableArray *sectionArray;

@property(nonatomic,strong)NSMutableArray *cellArray;
@end

@implementation accountBookVC

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self navig];
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    self.navigationController.navigationBarHidden=YES;
    
    self.cellArray = [[NSMutableArray alloc]init];
    
    NSDictionary *dic1 = [[NSDictionary alloc]initWithObjectsAndKeys:@"水电",@"name",@"-120.00",@"money",@"支出",@"type",@"10月20日 周四",@"date",@"水电",@"remark", nil];
    NSDictionary *dic2 = [[NSDictionary alloc]initWithObjectsAndKeys:@"工资",@"name",@"+60000.00",@"money",@"收入",@"type",@"10月20日 周四",@"date",@"工资",@"remark", nil];
    NSDictionary *dic3 = [[NSDictionary alloc]initWithObjectsAndKeys:@"零食",@"name",@"-100.00",@"money",@"支出",@"type",@"10月20日 周四",@"date",@"零食",@"remark", nil];
    NSDictionary *dic4 = [[NSDictionary alloc]initWithObjectsAndKeys:@"房租",@"name",@"-2000.00",@"money",@"支出",@"type",@"10月20日 周四",@"date",@"房租",@"remark", nil];
    self.cellArray = [[NSMutableArray alloc]initWithObjects:dic1,dic2,dic3,dic4, nil];
    
    
    NSDictionary *dic11 = [[NSDictionary alloc]initWithObjectsAndKeys:self.cellArray,@"array",@"10月21日 周五",@"data",@"收入：60000.00  支出：220.00",@"money", nil];
    NSDictionary *dic12 = [[NSDictionary alloc]initWithObjectsAndKeys:self.cellArray,@"array",@"10月20日 周四",@"data",@"收入：60000.00  支出：220.00",@"money", nil];
    NSDictionary *dic13 = [[NSDictionary alloc]initWithObjectsAndKeys:self.cellArray,@"array",@"10月19日 周三",@"data",@"收入：60000.00  支出：220.00",@"money", nil];
    NSDictionary *dic14 = [[NSDictionary alloc]initWithObjectsAndKeys:self.cellArray,@"array",@"10月18日 周二",@"data",@"收入：60000.00  支出：220.00",@"money", nil];
    
    self.sectionArray = [[NSMutableArray alloc]initWithObjects:dic11,dic12,dic13,dic14, nil];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.TabHeadView];
    [self.TabHeadView addSubview:self.titleLabel];
    [self.TabHeadView addSubview:self.rightButton];
    [self.TabHeadView addSubview:self.dataLabel];
    [self.TabHeadView addSubview:self.incomeLabel];
    [self.TabHeadView addSubview:self.expendLabel];
    [self.TabHeadView addSubview:self.dataBtn];
    [self.TabHeadView addSubview:self.income];
    [self.TabHeadView addSubview:self.expend];
    [self.view addSubview:self.Tab];
    
    // Do any additional setup after loading the view.
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}

-(void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    [super viewWillDisappear:animated];
}
#pragma mark-自定义导航区
-(void)navig{
    
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    self.navigationController.navigationBarHidden=YES;
    HomeNavigation*navView = [[HomeNavigation alloc]initWithTag:3 title:@"CM记账" handler:^(UIButton *sender) {
//        [self backPress];
    }];
    navView.img.backgroundColor = [UIColor colorWithHexString:@"#00CC9A"];
    navView.leftbut.hidden = YES;
    //    [navView addSubview:self.rightButton];
    [self.view addSubview:navView];
    
}
-(UILabel *)titleLabel{
    
    if (!_titleLabel) {
        
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,SafeAreaTopHeight - 44, ScreenWidth, 44)];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.textColor = [UIColor colorWithHexString:@"#FFFFFF"];
        [_titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:17*kfit]];
        _titleLabel.text = @"CM记账";
        
    }
    return _titleLabel;
}
-(UIButton *)rightButton{
    if (!_rightButton) {
        _rightButton = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth-60*kfit, SafeAreaTopHeight-40*kfit, 40*kfit, 40*kfit)];
        [_rightButton setImage:[UIImage imageNamed:@"签到-1"] forState:UIControlStateNormal];
        
        [_rightButton addTarget:self action:@selector(rightPress) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightButton;
}
-(UIView *)TabHeadView{
    if (!_TabHeadView) {
        _TabHeadView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenHeight, SafeAreaTopHeight+111*kfit)];
        _TabHeadView.backgroundColor = [UIColor colorWithHexString:@"#00CC9A"];;
    }
    return _TabHeadView;
}
-(UILabel *)dataLabel{
    if (!_dataLabel) {
        _dataLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, SafeAreaTopHeight + 15*kfit, 80*kfit, 20*kfit)];
        _dataLabel.textAlignment = NSTextAlignmentCenter;
        _dataLabel.font = [UIFont systemFontOfSize:12*kfit];
        _dataLabel.text = @"2018年";
        _dataLabel.textColor = [UIColor colorWithHexString:@"#D7D7D7"];
    }
    return _dataLabel;
}
-(UILabel *)incomeLabel{
    if (!_incomeLabel) {
        _incomeLabel = [[UILabel alloc]initWithFrame:CGRectMake(120*kfit, SafeAreaTopHeight + 15*kfit, 80*kfit, 20*kfit)];
//        _incomeLabel.textAlignment = NSTextAlignmentCenter;
        _incomeLabel.font = [UIFont systemFontOfSize:12*kfit];
        _incomeLabel.text = @"收入";
        _incomeLabel.textColor = [UIColor colorWithHexString:@"#D7D7D7"];
    }
    return _incomeLabel;
}
-(UILabel *)expendLabel{
    if (!_expendLabel) {
        _expendLabel = [[UILabel alloc]initWithFrame:CGRectMake(230*kfit, SafeAreaTopHeight + 15*kfit, 80*kfit, 20*kfit)];
        //        _incomeLabel.textAlignment = NSTextAlignmentCenter;
        _expendLabel.font = [UIFont systemFontOfSize:12*kfit];
        _expendLabel.text = @"支出";
        _expendLabel.textColor = [UIColor colorWithHexString:@"#D7D7D7"];
    }
    return _expendLabel;
}
-(UIButton *)dataBtn{
    if (!_dataBtn) {
        _dataBtn = [[UIButton alloc]initWithFrame:CGRectMake(0*kfit, SafeAreaTopHeight + 53*kfit, 80*kfit, 25*kfit)];
        [_dataBtn setTitle:@"10月" forState:0];
        [_dataBtn setTitleColor:[UIColor colorWithHexString:@"#ffffff"] forState:0];
        [_dataBtn setImage:[UIImage imageNamed:@"choose_down_black"] forState:UIControlStateNormal];
        // button标题的偏移量
        _dataBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -_dataBtn.imageView.bounds.size.width+2, 0, _dataBtn.imageView.bounds.size.width);
        // button图片的偏移量
        _dataBtn.imageEdgeInsets = UIEdgeInsetsMake(0, _dataBtn.titleLabel.bounds.size.width, 0, -_dataBtn.titleLabel.bounds.size.width);
        
//        [_dataBtn addTarget:self action:@selector(aaa) forControlEvents:UIControlEventTouchUpInside];
    }
    return _dataBtn;
}
-(UILabel *)income{
    if (!_income) {
        _income = [[UILabel alloc]initWithFrame:CGRectMake(120*kfit, SafeAreaTopHeight + 53*kfit, 80*kfit, 20*kfit)];
        //        _incomeLabel.textAlignment = NSTextAlignmentCenter;
        _income.font = [UIFont systemFontOfSize:18*kfit];
        _income.text = @"0.00";
        _income.textColor = [UIColor colorWithHexString:@"#ffffff"];
    }
    return _income;
}
-(UILabel *)expend{
    if (!_expend) {
        _expend = [[UILabel alloc]initWithFrame:CGRectMake(230*kfit, SafeAreaTopHeight + 53*kfit, 80*kfit, 20*kfit)];
        //        _incomeLabel.textAlignment = NSTextAlignmentCenter;
        _expend.font = [UIFont systemFontOfSize:18*kfit];
        _expend.text = @"0.00";
        _expend.textColor = [UIColor colorWithHexString:@"#ffffff"];
    }
    return _expend;
}

-(void)rightPress{
    
    calendarVC *VC = [[calendarVC alloc]init];
    self.navigationController.navigationBarHidden=NO;
    [VC setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:VC animated:YES];
    NJLog(@"日历");
}
-(UITableView *)Tab{
    
    if (!_Tab) {
        _Tab = [[UITableView alloc]initWithFrame:CGRectMake(0,self.TabHeadView.frame.size.height, ScreenWidth, ScreenHeight - SafeAreaTabbarHeight-self.TabHeadView.frame.size.height)];
        _Tab.backgroundColor = [UIColor colorWithHexString:@"#f5f5f5"];
        _Tab.delegate = self;
        _Tab.dataSource = self;
        //        self.myTab.scrollEnabled = NO;
        _Tab.showsVerticalScrollIndicator = NO;
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 50)];
        UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, ScreenWidth, 40)];
        lab.text = @"阿里云提供数据安全保障";
        lab.font = [UIFont systemFontOfSize:14*kfit];
        lab.textColor = [UIColor colorWithHexString:@"#999999"];
        lab.textAlignment = NSTextAlignmentCenter;
        [view addSubview:lab];
//        _Tab.tableFooterView = view;
//        _Tab.tableHeaderView = self.TabHeadView;
        _Tab.tableFooterView.backgroundColor = [UIColor colorWithHexString:@"#f5f5f5"];
        if(@available(iOS 11.0, *)){
            _Tab.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            _Tab.estimatedRowHeight = 0;
            _Tab.estimatedSectionHeaderHeight = 0;
            _Tab.estimatedSectionFooterHeight = 0;
            
        }
        
    }
    
    return _Tab;
}
#pragma mark -设置cell横线左顶边
-(void)viewDidLayoutSubviews {
    if ([self.Tab respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.Tab setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([self.Tab respondsToSelector:@selector(setLayoutMargins:)])  {
        [self.Tab setLayoutMargins:UIEdgeInsetsZero];
    }
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}
#pragma  make - UITableViewDataSource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.sectionArray.count;
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return [self.sectionArray[section][@"array"] count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    if (!cell) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        
    }
//    cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;
    cell.imageView.image = [UIImage imageNamed:@"我的摇钱桩购买"];
    cell.textLabel.textColor = [UIColor colorWithHexString:@"#222222"];
    cell.textLabel.font = [UIFont systemFontOfSize:16];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = self.sectionArray[indexPath.section][@"array"][indexPath.row][@"name"];
    cell.detailTextLabel.text = self.sectionArray[indexPath.section][@"array"][indexPath.row][@"money"];
    return cell;
}

#pragma  make - UITableViewDelegate
//返回高度，计算不同高度cell 一定要用，否则会出现偏移
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
 
    
    return 60;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view =[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 35)];
    view.backgroundColor = [UIColor colorWithHexString:@"#F6F6F8"];
    UILabel *leftLabel = [[UILabel alloc]initWithFrame:CGRectMake(21*kfit, 0, ScreenWidth/2, 35)];
    leftLabel.textColor = [UIColor colorWithHexString:@"#B7B7B7"];
    leftLabel.font = [UIFont systemFontOfSize:10*kfit];
    leftLabel.textAlignment = NSTextAlignmentLeft;
    leftLabel.text =self.sectionArray[section][@"data"];
    
    UILabel *rightLabel = [[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth/2 -21*kfit, 0, ScreenWidth/2, 35)];
    rightLabel.textColor = [UIColor colorWithHexString:@"#B7B7B7"];
    rightLabel.font = [UIFont systemFontOfSize:10*kfit];
    rightLabel.textAlignment = NSTextAlignmentRight;
    rightLabel.text =self.sectionArray[section][@"money"];
    
    [view addSubview:leftLabel];
    [view addSubview:rightLabel];
    
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 35;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    accountBookDetailVC *VC = [[accountBookDetailVC alloc]init];
    VC.detailDic = self.sectionArray[indexPath.section][@"array"][indexPath.row];
    self.navigationController.navigationBarHidden=NO;
    [VC setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:VC animated:YES];
  
}
    

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
